package top.haijunit.sample.navi.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.haijunit.common.response.api.ExcludeResponseWrapper;
import top.haijunit.sample.navi.service.QrCodeService;

/**
 * @author zhanghaijun
 * @date 2024/7/16 09:58
 * @description [一句话描述该类的功能]
 */
@RestController
@ExcludeResponseWrapper
@RequiredArgsConstructor
@RequestMapping("/code")
@Tag(name = "二维码生成")
public class QrCodeController {

    private final QrCodeService qrCodeService;

    @Operation(summary = "生成二维码", parameters = {
            @Parameter(name = "text", required = true, in = ParameterIn.QUERY, description = "二维码内容"),
            @Parameter(name = "width", in = ParameterIn.QUERY, description = "宽度"),
            @Parameter(name = "height", in = ParameterIn.QUERY, description = "高度"),
    })
    @GetMapping("/gen/qr")
    public ResponseEntity<byte[]> generateQrCode(@RequestParam String text,
                                                 @RequestParam(defaultValue = "200") int width,
                                                 @RequestParam(defaultValue = "200") int height) {
        byte[] qrCodeBytes = qrCodeService.generateQrCodeImage(text, width, height);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        headers.setContentDispositionFormData("attachment", "qrcode.png");

        return ResponseEntity.ok().headers(headers).body(qrCodeBytes);
    }
}
